package com.xh.bussiness.tms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xh.bussiness.permission.model.User;
import com.xh.bussiness.permission.service.IUserService;
import com.xh.bussiness.tms.bean.dto.ClassBean;
import com.xh.bussiness.tms.bean.request.ClassRequest;
import com.xh.bussiness.tms.bean.request.ClassStudentRequest;
import com.xh.bussiness.tms.service.IClassManageService;
import com.xh.bussiness.tms.service.IClassStudentService;
import com.xh.core.annotation.ExcludeLoginTokenIntercept;
import com.xh.core.bean.AjaxResult;
import com.xh.core.bean.XhPage;
import com.xh.core.constant.AjaxResultConstant;
import com.xh.bussiness.tms.model.Class;
import com.xh.core.util.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;


@Api(tags="班级管理接口")
@RestController
@RequestMapping("/class")
public class ClassManageController {

    @Autowired
    private IClassManageService classServce;
    @Autowired
    private IClassStudentService classStudentService;
    @Autowired
    private IUserService userService;

    @ApiOperation(value = "查询带分页的班级",notes = "查询带分页的班级接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name="grade_id", value="年级ID", required = true, dataType = "String"),
            @ApiImplicitParam(name="subject_id", value="学科ID", required = true, dataType = "String")
    })
    @PostMapping("/queryByPage")
    public XhPage<Class> queryByPage(XhPage page, ClassRequest bean){
        return classServce.queryByPage(page,bean);
    }

    @ApiOperation(value = "根据ID查询班级",notes = "根据ID查询班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id", value="班级ID", required = true, dataType = "String")
    })
    @PostMapping("/getClassById")
    public AjaxResult getClassById(String id){

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, classServce.getById(id));
    }

    @ApiOperation(value = "新增班级或者更新班级",notes = "新增班级或者更新班级")
    @PostMapping("/saveOrUpdate")
    public AjaxResult saveOrUpdate(@Validated Class bean){
        bean.setStatus(0);
        if(bean.getId()==null){
            classServce.insert(bean);
        }else{
            classServce.update(bean);
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",bean);
    }


    @ApiOperation(value = "删除班级",notes = "删除班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name="id", value="班级ID", required = true, dataType = "String")
    })
    @PostMapping("/deleteClass")
    @Transactional
    public AjaxResult deleteClass(String id){
        classServce.delete(id);

        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }


    @ApiOperation(value = "批量删除班级",notes = "批量删除班级")
    @ApiImplicitParams({
            @ApiImplicitParam(name="ids", value="班级ID", required = true, dataType = "String[]")
    })
    @PostMapping("/batchDelete")
    @Transactional
    public AjaxResult batchDelete(String[] ids){
        classServce.batchDelete(ids);
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    @ApiOperation(value = "查询所有班级",notes = "查询所有班级")
    @PostMapping("/queryAll")
    public AjaxResult queryAll(){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS,"",classServce.queryAll());
    }


    @ApiOperation(value = "班级选学生",notes = "班级选学生")
    @PostMapping("/addStudents")
    public AjaxResult addStudents(@RequestBody ClassStudentRequest classStudentRequest){
        return new AjaxResult(classServce.addStudents(classStudentRequest));
    }

    @RequestMapping("/queryByUserId")
    public AjaxResult queryByUserId(String subjectId) {
        String userId = UserUtils.getCurrentUserId();
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, classServce.queryClassByTeacherId(userId,subjectId));
    }

    @RequestMapping("/queryUserClassByUserThirdId")
    public AjaxResult queryUserClassByUserThirdId() {
        String currentUserId = UserUtils.getCurrentUserId();
        User byId = userService.getById(currentUserId);
        if (byId != null) {
            return new AjaxResult(AjaxResultConstant.Result.SUCCESS, classServce.queryClassByTeacherId(byId.getThirdId(), null));
        }
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS);
    }

    @ExcludeLoginTokenIntercept
    @ApiOperation(value = "查询班级学生",notes = "查询班级学生")
    @PostMapping("/queryStudentsByClassId")
    public AjaxResult queryStudentsByClassId(XhPage page, String classId, String name){
        return new AjaxResult(AjaxResultConstant.Result.SUCCESS, classServce.queryStudentsByClassId(page, classId, name));
    }


}
